/**
 * Main.java
 *
 * Created on Mar 7, 2010, 9:40:14 PM
 * 
 * @author Beatrice Bacelli
 * @author Luca Bedogni
 * @author Silvia Righini
 */

package unibo.lsb.graphic;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JFrame;

import unibo.lsb.exception.TokioException;
import unibo.lsb.server.RegistrationServerImpl;

/**
 * 
 */
public class MainWindow extends javax.swing.JFrame {

	/** Creates new form Main */
	public MainWindow() {
		initComponents();
	}

	public MainWindow(JFrame jf) {
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	private void initComponents() {

		setSize(new Dimension(500, 410));
		setLocationRelativeTo(null);

		lbl_claim = new javax.swing.JLabel();
		btn_createGame = new javax.swing.JButton();
		btn_joinGame = new javax.swing.JButton();
		btn_about = new javax.swing.JButton();
		lbl_icon = new javax.swing.JLabel();
		btn_servermode = new javax.swing.JButton();

		setTitle("Tokyo - by LSB");
		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		lbl_claim.setFont(new java.awt.Font("DejaVu Sans", 0, 36));
		lbl_claim.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		lbl_claim.setText("Tokyo - by LSB");
		lbl_claim.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

		btn_createGame.setText("Create Game");
		btn_createGame.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				btn_createGameClicked(e);
			}
		});

		btn_joinGame.setText("Join Game");
		btn_joinGame.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				btn_joinGamePressed(e);
			}
		});

		btn_about.setText("About");
		btn_about.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				btn_AboutPressed(e);
			}
		});

		URL imgUrl = getClass().getResource(
				ConfigurationConstGraphics.MAIN_PAGE_IMG);
		if (imgUrl != null) {
			lbl_icon.setIcon(new javax.swing.ImageIcon(imgUrl));
		}

		btn_servermode.setText("Server Mode");
		btn_servermode.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				btn_ServerModePressed(e);
			}

		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																lbl_claim,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																467,
																Short.MAX_VALUE)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addGroup(
																								layout
																										.createParallelGroup(
																												javax.swing.GroupLayout.Alignment.TRAILING)
																										.addComponent(
																												btn_createGame,
																												javax.swing.GroupLayout.PREFERRED_SIZE,
																												149,
																												javax.swing.GroupLayout.PREFERRED_SIZE)
																										.addGroup(
																												layout
																														.createParallelGroup(
																																javax.swing.GroupLayout.Alignment.LEADING)
																														.addComponent(
																																btn_joinGame,
																																javax.swing.GroupLayout.PREFERRED_SIZE,
																																149,
																																javax.swing.GroupLayout.PREFERRED_SIZE)
																														.addComponent(
																																btn_servermode,
																																javax.swing.GroupLayout.PREFERRED_SIZE,
																																149,
																																javax.swing.GroupLayout.PREFERRED_SIZE)))
																						.addComponent(
																								btn_about,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								149,
																								javax.swing.GroupLayout.PREFERRED_SIZE))
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				lbl_icon,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				300,
																				javax.swing.GroupLayout.PREFERRED_SIZE)))
										.addContainerGap()));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(
												lbl_claim,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												41,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addComponent(
																				btn_createGame,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				62,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addGap(
																				18,
																				18,
																				18)
																		.addComponent(
																				btn_servermode,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				62,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addGap(
																				18,
																				18,
																				18)
																		.addComponent(
																				btn_joinGame,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				62,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addGap(
																				18,
																				18,
																				18)
																		.addComponent(
																				btn_about,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				62,
																				javax.swing.GroupLayout.PREFERRED_SIZE))
														.addComponent(
																lbl_icon,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																301,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addContainerGap(32, Short.MAX_VALUE)));
	}

	private void btn_createGameClicked(ActionEvent evt) {
		this.setVisible(false);
		CreateGameWindow cgw = new CreateGameWindow();
		cgw.setVisible(true);
	}

	private void btn_ServerModePressed(ActionEvent evt) {
		this.setVisible(false);
		boolean exc = false;
		try {
			RegistrationServerImpl rsi = new RegistrationServerImpl();
			rsi.run();
		} catch (TokioException te) {
			exc = true;
			te.DisplayErrorInDialog(this);
		} catch (Exception ex) {
			Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE,
					null, ex);
		}
		// this is needed because server log window must be shown only if no
		// exception occurred
		// if we put this among the other settVisible window could be opened
		// just to show error dialog
		if (exc == false) {
			ServerWindowSingleton.getServerWindow().setVisible(true);
		}
	}

	private void btn_joinGamePressed(ActionEvent evt) {
		this.setVisible(false);
		RegistrationWindowSingleton.getInstance().getRegistrationWindow()
				.setVisible(true);
	}

	private void btn_AboutPressed(ActionEvent evt) {
		// this.setVisible(false);
		AboutDialog ad = new AboutDialog();
		ad.setVisible(true);
	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new MainWindow().setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton btn_about;
	private javax.swing.JButton btn_createGame;
	private javax.swing.JButton btn_joinGame;
	private javax.swing.JButton btn_servermode;
	private javax.swing.JLabel lbl_claim;
	private javax.swing.JLabel lbl_icon;
	// End of variables declaration//GEN-END:variables

}
